package classes;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class PasswordManager {
	/*
	 Given a byte[] array, produces a hex String,
	 such as "234a6f". with 2 chars for each byte in the array.
	 (provided code)
	*/
	public static String hexToString(byte[] bytes) {
		StringBuffer buff = new StringBuffer();
		for (int i=0; i<bytes.length; i++) {
			int val = bytes[i];
			val = val & 0xff;  // remove higher bits, sign
			if (val<16) buff.append('0'); // leading 0
			buff.append(Integer.toString(val, 16));
		}
		return buff.toString();
	}
	
	public PasswordManager() {
		// TODO Auto-generated constructor stub
	}
	
	
	public static String generatePassword(String password){
		//TODO: shesamowmebelia, shemowmeba + return;
		try {
			MessageDigest md = MessageDigest.getInstance("SHA");
			md.update(password.getBytes());
			return hexToString(md.digest());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	public static void sendForgottenPassword(String username,String pass){
		try {
			SendMail.send(username,"forgotten password",pass);			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	/**
	 * returns number password with numbers not less than 6 digit
	 */
	public static String getRandomPassword() {
		Random generator = new Random();
		return Integer.toString(generator.nextInt(999999) + 100000);
	}
}
